/**
 * 
 */
var BSTMG = BSTMG || {};
BSTMG.productmg = {
	init: function(){
		var that = this;
		/* 初始化树分类 */
		that.zTreeinit();
		/* 绑定新增、删除按钮操作 */
		that.bindOperate();
		/* 表格操作 */
		that.tableOperate();
		/* 新增分类 */
		that.addNewCategory();
	},
	addNewCategory: function(){
		var $box = $("#addcategorybox"),
			$iptcategory = $box.find("#choosecategory");
		$iptcategory.on("click",".input-group-addon",function(){
			showCategory();
		}).on("click","input.form-control",function(){
			showCategory();
		}).on("keydown","input.form-control",function(){
			return false;
		});
		
		function showCategory(){
			var dialog = null;
			var html = '<div id="chooseproduct"><table class="table table-bordered chopdtable"><thead><tr><th><lable class="icheck checkall"><input type="checkbox" class="simple"> <span>全选</span></lable></th><th><span class="filter filterno">产品编码</span></th><th><span class="filter filtername">产品名称</span></th><th><span class="filter filtercty">产品中文名</span></th><th class="producturl"><span class="filter filtertime">产品URL</span></th></tr></thead><tbody><tr><td><input type="checkbox" class="simple"></td><td>b</td><td>阿毛</td><td>123</td><td>哈哈</td></tr><tr><td><input type="checkbox" class="simple"></td><td>a</td><td>不错</td><td>321</td><td>呵呵</td></tr></tbody></table></div>';
			dialog = BSTYUN.dialog.creat({
				id : "categories",
				title: "选择产品",
				content : html,
				top: "58px",
				init: initChooseProducts
			});
			dialog.button(
				{
					name : "获取最新产品列表",
					callback : getNewlyProducts
				},
				{
					name : "确定",
					callback : getConfirmResult
				},{
					name : "取消"
				}
			);
		};
		// 初始化对话框
		function initChooseProducts(){
			var $content = this.DOM.content,
				$thead = $content.find("thead"),
				$tbody = $content.find("tbody");
			// 全选
			$thead.on("click",".checkall",function(){
				var $this = $(this),
				$current = $this.find(".simple"),
				$parent = $this.parents('table.chopdtable'),
				$checkbox = $parent.find('input[type=checkbox]').not($current);
				$current.prop("checked") ? $checkbox.prop("checked",true) : $checkbox.prop("checked",false) ;
			});
			$tbody.on("click","input[type=checkbox]",function(){
				var $this = $(this),
					$all = $this.parents("tbody").eq(0).find("input[type=checkbox]"),
					$checkbox = $all.not($this).filter(":checked");
				var $checkall = $thead.find(".checkall input[type=checkbox]");
				( $this.prop("checked") && ($checkbox.length + 1) == $all.length ) ? $checkall.prop("checked",true) : $checkall.prop("checked",false) ;
			});
		};
		// 获取最新产品列表
		function getNewlyProducts(){
			alert('获取最新产品列表')
		};
		// 确定新增分类
		function getConfirmResult(){
			alert('ajax提交分类、dom写入')
		};
	},
	tableOperate: function(){
		var $operator = $("#operator"),
			$table = $("#categorytable"),
			$thead = $table.find("thead"),
			$tbody = $table.find("tbody");
		// 全选
		$thead.on("click",".checkall",function(){
			var $this = $(this),
				$current = $this.find(".simple"),
				$parent = $this.parents('table#categorytable'),
				$checkbox = $parent.find('input[type=checkbox]').not($current);
			$current.prop("checked") ? $checkbox.prop("checked",true) : $checkbox.prop("checked",false) ;
		});
		$tbody.on("click","input[type=checkbox]",function(){
			var $this = $(this),
				$all = $this.parents("tbody").eq(0).find("input[type=checkbox]"),
				$checkbox = $all.not($this).filter(":checked");
			var $checkall = $thead.find(".checkall input[type=checkbox]");
			($this.prop("checked") && ($checkbox.length + 1) == $all.length ) ? $checkall.prop("checked",true) : $checkall.prop("checked",false) ;
		});
		// 删除商品分类
		$operator.on("click",".deletecategory",function(){
			var $checkbox = $tbody.find("input.simple").filter(":checked");
			if($checkbox.length){
				BSTYUN.dialog.confirm("确定要删除吗？",deleteCategory);	
			}else{
				BSTYUN.dialog.warn("请选择要删除的选项！");
			}
		});
		// 删除选项
		function deleteCategory(){
			
		};
		// 筛选
		$thead.on("click",".filter",function(){
			var $this = $(this),
				$dir = $this.next(),
				index = $this.parents("tr").children().index($this.parent()),
				store = [],
				sift = null,
				desc = $dir.hasClass("fa-sort-desc");
			
			$tbody.find('tr').each(function(sub,val){
				var $tr = $(this),
					txt = $tr.find("td").eq(index).text();
				var obj = {
						tr : $tr,
						txt : txt
				}
				store.push(obj);
			});
			if(desc){
				sift = function(a,b){return a.txt > b.txt ? 1 : -1;}
				$dir.removeClass("fa-sort-desc").addClass("fa-sort-up");
			}else{
				sift = function(a,b){return a.txt > b.txt ? -1 : 1;} 
				$dir.removeClass("fa-sort-up").addClass("fa-sort-desc");
			}
			$.each( store.sort(sift) , function(){
				$tbody.append(this.tr);
			});
		});
		// 查看编辑删除
		$tbody.on("click",".categorylook",function(){
			var $this = $(this);

		}).on("click",".categoryedit",function(){
			var $this = $(this);
			
		}).on("click",".categorydelete",function(){
			var $this = $(this);
			
		});
	},
	bindOperate: function(){
		var that = this,
			$operator = $("#operator"),
			$addoperator = $("#addoperator");
		var $categorylist = $("#categorylist"),
			$addcategorybox = $("#addcategorybox");
		var $area = {
				"categorylist": $categorylist,
				"addcategorybox": $addcategorybox
		}
		// 新增下级分类 
		$operator.on("click",".addcategory",function(){
			showArea('addcategorybox');
		});
		// 确定取消 
		$addoperator.on("click",".confirm",function(){
			//showArea('addcategorybox');
		}).on("click",".cancel",function(){
			showArea('categorylist');
		});
		/*
		 * showArea 展示区块，参数为类名
		 * */
		function showArea(area){
			for(var key in $area){
				key == area ? $area[key].show() : $area[key].hide();
			}
		}
	},
	zTreeinit: function(){
		var $treebox = $("#treeDemo");
		var setting = {
				view: {
					showIcon: showIconForTree
				},
				data: {
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pId",
						rootPId: 0
					}
				}
			};
		var zNodes = SYSTEM.zNodes;
		function showIconForTree(treeId, treeNode) {
			return !treeNode.isParent;
		};
		if($.isArray(zNodes)){
			$.fn.zTree.init($treebox, setting, zNodes);
		}else{
			$(".showcategory").html("<span style='margin-left:20px;'>数据加载出错！</span>");
		}
		
	}
};

$(function(){
	BSTMG.productmg.init();
});